﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace AlarmClock
{
    public partial class FrmAutoBerthForm : Form
    {
        public int formWidth;
        public int formHeight;
        public int screenWidth;
        public int screenHeight;
        /**
         *flag=0:屏幕左
         *flag=1:屏幕上
         *flag=2:屏幕右
         *flag=3:屏幕下
         *flag=4:屏幕中
         */
        public int flag;
        public int location_Left;
        public int location_Top;

        //private bool isAutoBerth=true;
        public bool isShow = true;
        public bool isMouseDown = false;

        public FrmAutoBerthForm()
        {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        private void AutoBerthForm_Load(object sender, EventArgs e)
        {
            timCheckMouseState_Tick(sender, e);
            getState();
        }

        private void AutoBerthForm_LocationChanged(object sender, EventArgs e)
        {
            Screen screen = Screen.PrimaryScreen;
            screenWidth = screen.Bounds.Width;
            screenHeight = screen.Bounds.Height;
            formWidth = this.Width;
            formHeight = this.Height;
            location_Left = this.Left;
            location_Top = this.Top;
            getState();
        }

        private void timCheckMouseState_Tick(object sender, EventArgs e)
        {
            if (flag < 4)
            {
                if (isShow)
                {
                    //窗体显示时，鼠标指针是否离开窗体所在的区域
                    if (!this.Bounds.Contains(Cursor.Position))
                        autoBerth();
                }
                else
                {
                    //窗体隐藏时，鼠标指针是否进入窗体附近的区域
                    if (this.Bounds.Contains(Cursor.Position))
                        autoShow();
                }
            }
        }

        public void getState()
        {
            if (location_Left < 8)
            {
                if (location_Top < location_Left)
                    flag = 1;
                else if (screenHeight - location_Top - formHeight < location_Left)
                    flag = 3;
                else flag = 0;
                return;
            }
            if (location_Top < 8)
            {
                if (screenWidth - location_Left - formWidth < location_Top)
                    flag = 2;
                else
                    flag = 1;
                return;
            }
            if (location_Left + formWidth > screenWidth - 8)
            {
                if (screenWidth - location_Left - formWidth > screenHeight - location_Top - formHeight)
                    flag = 3;
                else
                    flag = 2;
                return;
            }
            if (location_Top + formHeight > screenHeight - 8)
            {
                flag = 3;
                return;
            }
            flag = 4;
            return;
        }

        public void autoBerth()
        {
            timCheckMouseState.Stop();
            int step = 2;
            switch (flag)
            {
                case 0:
                    while (this.Left + formWidth > 3)
                    {
                        Thread.Sleep(2);
                        this.Left -= step;
                    }
                    break;
                case 1:
                    step = formHeight / formWidth * 2;
                    while (this.Top + formHeight > 3)
                    {
                        Thread.Sleep(1);
                        this.Top -= step;
                    }
                    break;
                case 2:
                    while (this.Left<screenWidth-3)
                    {
                        Thread.Sleep(1);
                        this.Left += step;
                    }
                    break;
                case 3:
                    step = formHeight / formWidth * 2;
                    while (this.Top < screenHeight - 3)
                    {
                        Thread.Sleep(2);
                        this.Top += step;
                    }
                    break;
                default:
                    break;
            }
            isShow = false;
            timCheckMouseState.Start();
        }

        public void autoShow()
        {
            timCheckMouseState.Stop();
            int step = 10;
            switch (flag)
            {
                case 0:
                    while (this.Left< step * -1)
                    {
                        Thread.Sleep(2);
                        this.Left += step;
                    }
                    this.Left = 0;
                    break;
                case 1:
                    while (this.Top < step * -1)
                    {
                        Thread.Sleep(1);
                        this.Top += step;
                    }
                    this.Top = 0;
                    break;
                case 2:
                    while (this.Left + formWidth > screenWidth + step)
                    {
                        Thread.Sleep(2);
                        this.Left -= step;
                    }
                    this.Left = screenWidth - formWidth;
                    break;
                case 3:
                    while (this.Top + formHeight > screenHeight + step)
                    {
                        Thread.Sleep(1);
                        this.Top -= step;
                    }
                    this.Top = screenHeight - formHeight;
                    break;
                default:
                    break;
            }
            isShow = true;
            timCheckMouseState.Start();
        }
    }
}
